package com.wisemoresystem.seal.action.job;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import org.ne.framework.data.util.ChkTools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.wisemoresystem.seal.base.action.ProjBaseAction;
import com.wisemoresystem.seal.base.tip.Tip;
import com.wisemoresystem.seal.model.t_bank_job;
import com.wisemoresystem.seal.service.JobService;
import com.wisemoresystem.seal.util.json.JsonTools;
import com.wisemoresystem.seal.web.WebTools;

/**
 * 职位管理
 * 
 * @author Administrator
 *
 */
@Controller("ixJoba")
@Scope("prototype")
@RequestMapping("/ix/job")
public class JobAction extends ProjBaseAction {

	@Autowired
	private JobService lService;

	/**
	 * 职位列表
	 * 
	 * @param location_id
	 * @return
	 */
	@RequestMapping("/list")
	public ModelAndView list(String location_id) {

		List<Map<String, Object>> list = lService.selectLocation();
		String locationJson = JsonTools.toJson(list);

		System.err.println(locationJson);

		ModelAndView mv = new ModelAndView("/jsp/job/tree");
		mv.addObject("locationJson", locationJson);
		mv.addObject("location_id", location_id);
		return mv;
	}

	/**
	 * 异步加载职位
	 * 
	 * @param location_id
	 * @throws IOException
	 */
	@RequestMapping("/async_list")
	public void async_list(String location_id) throws IOException {
		resp.setCharacterEncoding("utf-8");

		List<Map<String, Object>> list = lService
				.selectLocationByAsync(location_id);

		String locationJson = JsonTools.toJson(list);

		resp.getWriter().print(locationJson);
		resp.getWriter().flush();
	}

	/**
	 * 通过code查找职位
	 * 
	 * @param code
	 */
	@RequestMapping("/get_location")
	@ResponseBody
	public String get_location(String code) throws IOException {

		t_bank_job location = lService.selectLocationByCode(code);
		System.out.println(code + "---" + location.getName());
		return location.getName();
	}

	@RequestMapping("/add_form")
	public ModelAndView add_form() {

		ModelAndView mv = new ModelAndView("/jsp/job/add_form");
		return mv;
	}

	/**
	 * 添加职位
	 * 
	 * @return
	 */
	@RequestMapping("/add")
	public ModelAndView add(t_bank_job location) {

		location.setUser_id(WebTools.getCurrentUserId());
		if (ChkTools.isNull(location.getSuper_id())) {
			location.setSuper_id("root");
		}
		oc.saveObj(location);

		logT.logH("添加职位", t_bank_job.class, location.get_id());

		super.setTipMsg("保存成功！", Tip.Type.success);
		ModelAndView mv = new ModelAndView("redirect:list.htm");
		mv.addObject("location_id", location.getSuper_id());
		return mv;
	}

	@RequestMapping("/edit_form")
	public ModelAndView edit_form(String location_id) {

		t_bank_job tLoc = oc.findObjById(location_id, t_bank_job.class);

		ModelAndView mv = new ModelAndView("/jsp/job/edit_form");
		mv.addObject("location", tLoc);
		return mv;
	}

	/**
	 * 修改职位
	 * 
	 * @return
	 */
	@RequestMapping("/edit")
	public ModelAndView edit(String id, t_bank_job location) {

		t_bank_job tLoc = oc.findObjById(id, t_bank_job.class);
		tLoc.setCode(location.getCode());
		tLoc.setName(location.getName());
		oc.updateObj(tLoc);

		logT.logH("修改职位", t_bank_job.class, tLoc.get_id());

		super.setTipMsg("修改成功！", Tip.Type.success);
		ModelAndView mv = new ModelAndView("redirect:list.htm");
		mv.addObject("location_id", id);
		return mv;
	}

	/**
	 * 删除职位
	 * 
	 * @return
	 */
	@RequestMapping("/del")
	public ModelAndView edit(String id) {

		t_bank_job tLoc = oc.findObjById(id, t_bank_job.class);
		boolean islive = lService.isLive(id);
		if (islive) {
			super.setTipMsg("删除不成功！存在下级职位！", Tip.Type.warning);
		} else {
			oc.delObj(tLoc);
			super.setTipMsg("删除成功！", Tip.Type.success);
			logT.logH("删除职位", t_bank_job.class, tLoc.get_id());
		}

		ModelAndView mv = new ModelAndView("redirect:list.htm");
		return mv;
	}

	/**
	 * 添加子职位
	 * 
	 * @return
	 */
	@RequestMapping("/add_child")
	public ModelAndView add_child(String id) {

		t_bank_job tLoc = oc.findObjById(id, t_bank_job.class);

		ModelAndView mv = new ModelAndView("/jsp/job/add_child");
		mv.addObject("location", tLoc);
		return mv;
	}

}
